package com.trust.pams.service.transactional.impl;

import com.chasonx.common.util.Ret;
import com.trust.pams.dao.mapper.PamsQuoteMapper;
import com.trust.pams.dao.mapper.PamsUserMapper;
import com.trust.pams.dao.service.*;
import com.trust.pams.domain.PamsQuote;
import com.trust.pams.domain.PamsQuoteExample;
import com.trust.pams.domain.PamsUser;
import com.trust.pams.domain.PamsUserExample;
import com.trust.pams.dto.PamsClientDto;
import com.trust.pams.dto.PamsMilestoneDto;
import com.trust.pams.dto.PamsQuoteDto;
import com.trust.pams.model.es.ClientEsSearch;
import com.trust.pams.model.es.MilestoneEsSearch;
import com.trust.pams.model.es.QuoteEsSearch;
import com.trust.pams.service.redis.config.PamsQuoteCaptcha;
import com.trust.pams.service.redis.pub.PubService;
import com.trust.pams.service.transactional.PamsQuoteTransactional;
import com.trust.pams.service.util.TrutsPamsImportUtil;
import com.trust.pams.util.DataUtil;
import com.trust.pams.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class PamsQuoteTransactionalImpl implements PamsQuoteTransactional {

    @Autowired
    PamsPuoteDao pamsPuoteDao;

    @Autowired
    private PamsQuoteMapper pamsQuoteMapper;

    @Autowired
    EsPamsPuoteIdDao esPamsPuoteIdDao;

    @Autowired
    private PubService pubService;

    @Autowired
    private EsPamsClientDao esPamsClientDao;

    @Autowired
    private PamsClientDao pamsClientDao;

    @Autowired
    private PamsUserMapper pamsUserMapper;

    @Autowired
    private EsPamsMilestoneDao esPamsMilestoneDao;

    @Autowired
    private PamsMilestoneDao pamsMilestoneDao;

    @Override
    public Ret batchPamsQuote(String filePath) {
        List<PamsQuoteDto> listQuote = TrutsPamsImportUtil.readQupte(filePath);
        List<PamsQuoteDto> pamsQuoteDtos= new ArrayList<>();
        PamsQuote quote = new PamsQuote();

        List<QuoteEsSearch> itemEsSearchList = new ArrayList<>();
        List<PamsQuoteCaptcha> pamsQuoteCaptchaList = new ArrayList<>();
        int b= 0;
        for(PamsQuoteDto quoteDto : listQuote){
            QuoteEsSearch quoteEsSearch = new QuoteEsSearch();
            PamsQuoteExample pamsQuoteExample = new PamsQuoteExample();

            pamsQuoteExample.createCriteria().andProjectversionsEqualTo(quoteDto.getProjectId());
            List<PamsQuote>  pamsQuoteList =  pamsQuoteMapper.selectByExample(pamsQuoteExample);
            if(pamsQuoteList.size()>=1){
                String pjGuid="";
                String id="";
                for(PamsQuote que: pamsQuoteList){
                    pjGuid=que.getProjectguid();
                    id=que.getId();
                }
                pamsPuoteDao.deletePamsPuote(pjGuid);
                pamsQuoteMapper.deleteByPrimaryKey(id);
                esPamsPuoteIdDao.dleteDocument(quoteDto.getProjectId(),"es_pams");
            }else {
                b++;
            }
            quote.setProjectname(quoteDto.getProjectName());
            quote.setId(quoteDto.getId());
            quote.setProjectguid(quoteDto.getProjectGuid());
            quote.setProjectversions(quoteDto.getProjectId());
            pamsQuoteMapper.insert(quote);
            quoteEsSearch.setId(quoteDto.getId());
            quoteEsSearch.setProjectGuid(quoteDto.getProjectGuid());
            quoteEsSearch.setProjectName(quoteDto.getProjectName());
            quoteEsSearch.setProjectId(quoteDto.getProjectId());
            quoteEsSearch.setRows(quoteDto.getRows());
            itemEsSearchList.add(quoteEsSearch);
            PamsQuoteCaptcha pamsQuoteCaptcha = new PamsQuoteCaptcha();
            pamsQuoteCaptcha.setProjectId(quoteDto.getProjectId());
            pamsQuoteCaptcha.setProjectGuid(quoteDto.getProjectGuid());
            pamsQuoteCaptchaList.add(pamsQuoteCaptcha);
        }
        List<PamsQuoteCaptcha> reidsList= removeDuplicate(pamsQuoteCaptchaList);
        for(PamsQuoteCaptcha pamsQuoteCaptcha:reidsList){
            pubService.pubMessage(pamsQuoteCaptcha);
        }
        System.out.println("项目编号"+b);
        int res = pamsPuoteDao.batchPamsPuote(listQuote);
        boolean a= esPamsPuoteIdDao.batchAddDocument(itemEsSearchList,"es_pams");
        return Ret.get().success();
    }

    public Ret batchPamsClient(String filePath){
        List<PamsClientDto> pamsClientDtos= TrutsPamsImportUtil.readClient(filePath);
        List<ClientEsSearch> clientEsSearches= new ArrayList<>();
        for(PamsClientDto clientDto:pamsClientDtos){
            ClientEsSearch clientEsSearch= new ClientEsSearch();
            clientEsSearch.setClientName(clientDto.getClientName());
            clientEsSearch.setClientBasic(clientDto.getClientBasic());
            clientEsSearch.setClientDetails(clientDto.getClientDetails());
            clientEsSearches.add(clientEsSearch);
            pamsClientDao.deletePamsClient(clientDto.getClientName());
            esPamsClientDao.dleteDocument(clientDto.getClientName(),"es_pams");
        }
        pamsClientDao.batchPamsClient(pamsClientDtos);
        esPamsClientDao.batchAddDocument(clientEsSearches,"es_pams");
        return Ret.get().success();
    }

    public Ret batchPamsUser(String filePath){
        List<PamsUser> pamsUserList =  TrutsPamsImportUtil.readUser(filePath);
        for(PamsUser pamsUser:pamsUserList){
            PamsUserExample pamsUserExample = new PamsUserExample();
            pamsUserExample.createCriteria().andNameEqualTo(pamsUser.getName());
            pamsUserMapper.deleteByExample(pamsUserExample);
            pamsUserMapper.insert(pamsUser);
        }
        return Ret.get().success();
    }

    public Ret batchPamsMilestone(String filePath){
        List<PamsMilestoneDto> dtoList = TrutsPamsImportUtil.readMilestone(filePath);
        List<MilestoneEsSearch> itemEsSearchList = new ArrayList<>();
        for(PamsMilestoneDto pamsMilestoneDto:dtoList){
            MilestoneEsSearch milestoneEsSearch = new MilestoneEsSearch();
            milestoneEsSearch.setId(pamsMilestoneDto.getId());
            milestoneEsSearch.setProjectId(pamsMilestoneDto.getProjectId());
            milestoneEsSearch.setRows(pamsMilestoneDto.getRows());
            itemEsSearchList.add(milestoneEsSearch);
            esPamsMilestoneDao.dleteDocument(pamsMilestoneDto.getProjectId(),"es_pams");
            pamsMilestoneDao.deletePamsMilestone(pamsMilestoneDto.getProjectId());
        }
        esPamsMilestoneDao.batchAddDocument(itemEsSearchList,"es_pams");
        pamsMilestoneDao.batchPamsMilestone(dtoList);
        return Ret.get().success();
    }

    public   static   List<PamsQuoteCaptcha>  removeDuplicate(List<PamsQuoteCaptcha> list)  {
        for (int i =0;i<list.size();i++){
            for(int j=i+1;j<list.size();j++){
                String a =  list.get(i).getProjectId();
                String b = list.get(j).getProjectId();
                if  (a.equals(b))  {
                    list.remove(j);
                }
            }
        }
        return list;
    }

}
